package yunjiao.javatutorials.designpatterns.interpreter;

import java.util.Map;

/**
 * TODO
 *
 * @author yangyunjiao
 */
public abstract class NonTerminalExpression implements Expression {
    protected final Expression left;
    protected final Expression right;

    public NonTerminalExpression(Expression left, Expression right) {
        this.left = left;
        this.right = right;
    }

    public static class AddExpression extends NonTerminalExpression {

        public AddExpression(Expression left, Expression right) {
            super(left, right);
        }

        @Override
        public int interpret(Map<String, Integer> context) {
            return left.interpret(context) + right.interpret(context);
        }
    }

    public static class SubtractExpression extends NonTerminalExpression {

        public SubtractExpression(Expression left, Expression right) {
            super(left, right);
        }

        @Override
        public int interpret(Map<String, Integer> context) {
            return left.interpret(context) - right.interpret(context);
        }
    }
}
